<template>
  <el-dialog v-el-drag-dialog :visible.sync="dialog" :close-on-click-modal="false" :title="title" append-to-body width="600px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
      <el-form-item v-if="!isRoot" label="菜单图标" prop="icon">
        <el-popover
          placement="bottom-start"
          width="460"
          trigger="click"
          @show="$refs['iconSelect'].reset()"
        >
          <IconSelect ref="iconSelect" @selected="selected"/>
          <el-input
            slot="reference"
            v-model="form.icon"
            placeholder="点击选择图标"
            readonly
            class="el-input-width"
          >
            <svg-icon
              v-if="form.icon"
              slot="prefix"
              :icon-class="form.icon"
              class="el-input__icon"
              style="height: 32px;width: 16px;"
            />
            <i v-else slot="prefix" class="el-icon-search el-input__icon"/>
          </el-input>
        </el-popover>
      </el-form-item>
      <el-form-item label="菜单名称" prop="name">
        <el-input v-model="form.name" placeholder="名称" class="el-input-width"/>
      </el-form-item>
      <el-form-item label="菜单排序" prop="sort">
        <el-input-number
          v-model.number="form.sort"
          :min="0"
          :max="999"
          controls-position="right"
          class="el-input-width"
        />
      </el-form-item>
      <el-form-item label="链接地址">
        <el-input v-model="form.path" placeholder="链接地址" class="el-input-width"/>
      </el-form-item>
      <el-form-item label="组件路径">
        <el-input v-model="form.template" placeholder="组件路径" class="el-input-width"/>
      </el-form-item>
      <el-form-item label="是否隐藏" prop="isHidden">
        <el-radio v-model="form.hidden" :label="true">是</el-radio>
        <el-radio v-model="form.hidden" :label="false">否</el-radio>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { menu_add, menu_update } from '@/api/system';
import IconSelect from '@/components/IconSelect';
import elDragDialog from '@/directive/el-drag-dialog';
export default {
  components: { IconSelect },
  directives: { elDragDialog },
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      loading: false,
      dialog: false,
      menus: [],
      form: {
        uuid: null,
        parentUuid: null,
        hidden: false,
        name: '',
        sort: 0,
        path: '',
        template: '',
        icon: ''
      },
      isRoot: false,
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        icon: [{ required: true, message: '请选择图标', trigger: 'change' }],
        sort: [
          {
            required: true,
            message: '请输入序号',
            trigger: 'blur',
            type: 'number'
          }
        ]
      }
    };
  },
  methods: {
    cancel() {
      this.resetForm();
    },
    doSubmit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.isAdd) {
            this.doAdd();
          } else this.doEdit();
        } else {
          return false;
        }
      });
    },
    async doEdit() {
      await menu_update(this.form);
      this.resetForm();
      this.$message({
        message: '修改成功',
        type: 'success'
      });
      this.loading = false;
      this.$parent.getMenus();
    },
    async doAdd() {
      await menu_add(this.form);
      this.resetForm();
      this.$message({
        message: '添加成功',
        type: 'success'
      });
      this.loading = false;
      this.$parent.getMenus();
    },
    resetForm() {
      this.dialog = false;
      // this.$refs['form'].resetFields();
      this.form = {
        uuid: null,
        parentUuid: null,
        hidden: false,
        name: '',
        sort: 0,
        path: '',
        template: '',
        icon: ''
      };
    },
    selected(name) {
      this.form.icon = name;
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
/deep/ .el-input-number .el-input__inner {
  text-align: left;
}
</style>
